package com.cqs.leetcode.search.binary;

import java.util.Arrays;

/**
 * lixw
 * 2023/11/10
 */
public class Solution2300 {

    public int[] successfulPairs(int[] spells, int[] potions, long success) {
        Arrays.sort(potions);
        int[] result = new int[spells.length];
        for (int i = 0; i < spells.length; i++) {
            result[i] = size(spells[i], potions, success);
        }
        return result;
    }


    private int size(int n, int[] potions, long success) {
        int l = 0, r = potions.length - 1;
        while (l <= r) {
            int mid = (r - l) / 2 + l;
            long tmp = potions[mid] * (long)n;
            //找到第一个使得
            if (tmp < success) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        //终止条件是r一定到最左边满足条件的前面一个
        return (potions.length - 1) - r;
    }
}
